/*****************************************************************
 *
 * (c) Developed by Zhang BaiQiang. All rights reserved.
 *
 *  update version 1.0 2018-08-24
 *
 *
 *
 *
 *
 *
 *****************************************************************/

package com.hydl.power.utils;


import lombok.extern.slf4j. Slf4j;

/**
 * 进制转换工具类
 * <p>
 * Title: 进制转换工具类
 * </p>
 * <p>
 * Description: 二进制、十进制、十六进制、byte字节数组之间的相互转换
 * </p>
 * <p>
 * Company: www.zzboyaxun.com
 * </p>
 *
 * @version 1.0
 * @since 2018年08月24号
 */
@Slf4j
public class ConvertUtils {

    /**
     * 错误代码
     */
    private static final String ERROR_VALUE = "0x00";

    /**
     * 十进制字符串转十六进制字符串
     *
     * @param msg 待转换的十进制字符串
     * @return 转换完成的十六进制字符串
     * @since 2018年08月24号
     */
    public static final String string2HexString(String msg) {
        char[] chars = "0123456789ABCDEF".toCharArray();
        StringBuilder sb = new StringBuilder("");
        byte[] bs = msg.getBytes();
        int bit;
        for (int i = 0; i < bs.length; i++) {
            bit = (bs[i] & 0x0f0) >> 4;
            sb.append(chars[bit]);
            bit = bs[i] & 0x0f;
            sb.append(chars[bit]);
        }
        return sb.toString();
    }

    /**
     * byte[]字节数组转16进制字符串
     *
     * @param record 待转换的字节数组
     * @return 转换完成的十六进制字符串
     * @since 2018年08月24号
     */
    public static final String bytesToHexString(byte[] record) {
        StringBuffer sb = new StringBuffer(record.length);
        String sTemp;
        for (int i = 0; i < record.length; i++) {
            sTemp = Integer.toHexString(0xFF & record[i]);
            if (sTemp.length() < 2) {
                sb.append(0);
            }
            sb.append(sTemp.toUpperCase() + " ");
        }
        return sb.toString();
    }

    /**
     * 16进制字符串转字节数组
     *
     * @param hexString 待转换的十六进制字符串
     * @return 转换完成的字节数组
     * @author ZhangBaiQiang
     * @since 2018年08月24号
     */
    public static final byte[] hexStringToBytes(String hexString) {
        if (hexString == null || "".equals(hexString)) {
            return null;
        }
        //去除空格，转换大写
        hexString = hexString.replace(" ", "").trim().toUpperCase();
        //获取字节数组长度
        int length = hexString.length() / 2;
        char[] hexChars = hexString.toCharArray();
        byte[] d = new byte[length];
        for (int i = 0; i < length; i++) {
            int pos = i * 2;
            d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));
        }
        return d;
    }

    /**
     * 十六进制字符串转十进制字符串
     *
     * @param hexStr 待转换的十六进制字符串
     * @return 转换完成的十进制字符串
     * @author ZhangBaiQiang
     * @since 2018年08月24号
     */
    public static final String hexString2String(String hexStr) {
        String str = "0123456789ABCDEF";
        char[] hexs = hexStr.toCharArray();
        byte[] bytes = new byte[hexStr.length() / 2];
        int n;
        for (int i = 0; i < bytes.length; i++) {
            n = str.indexOf(hexs[2 * i]) * 16;
            n += str.indexOf(hexs[2 * i + 1]);
            bytes[i] = (byte) (n & 0xff);
        }
        return new String(bytes);
    }

    /**
     * 字符转字节
     *
     * @param c 待转换的字符
     * @return 转换完成的字节
     * @author ZhangBaiQiang
     * @since 2018年08月24号
     */
    @SuppressWarnings("all")
    public static final byte charToByte(char c) {
        return (byte) "0123456789ABCDEF".indexOf(c);
    }

    /**
     * int数值转换字节数组
     * <p>
     * 后期可扩展到4294967295
     * </p>
     * <p>
     * %02x(00-FF,0-255)
     * </p>
     * <p>
     * %04x(0000-FFFF,0-65535)
     * </p>
     * <p>
     * %06x(000000-FFFFFF,0-16777215)
     * </p>
     * <p>
     * %08x(00000000-FFFFFFFF,4294967295)
     * </p>
     *
     * @param val  待转换的数值ֵ
     * @param type 需要转换的字节数组长度
     * @return 十六进制字符串(string.length = 2 | | 4)
     * @author ZhangBaiQiang
     * @since 2018年08月24号
     */
    public static final String int2Bytes(int val, int type) {
        String convert_str = "";
        switch (type) {
            case 1:
                if (val <= 255) {
                    convert_str = String.format("%02x", val);
                } else {
                    convert_str = ERROR_VALUE;
                }
                break;
            case 2:
                if (val <= 65535) {
                    convert_str = String.format("%04x", val);
                } else {
                    convert_str = ERROR_VALUE;
                }
                break;
            case 3:
                if (val <= 16777215) {
                    convert_str = String.format("%06x", val);
                } else {
                    convert_str = ERROR_VALUE;
                }
                break;
            default:
                convert_str = ERROR_VALUE;
                break;
        }
        return convert_str;
    }


    public static void main(String[] args) {

        log.info("1>" + hexString2String("00"));
        log.info(String.valueOf(Integer.valueOf("30", 16)));
        Integer i = 0;
        byte[] b = new byte[1];
        b[0] = i.byteValue();
        log.info(bytesToHexString(b));

        log.info(String.valueOf("1A 90".equals("1A 90")));

    }


}
